/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var priceList="";
var invoce="";
var i=0;
$(document).ready(function()
{
    $.getJSON('data/table.price.json',function(json){priceList=json});
    $.getJSON('data/table.testdata.json',function(json){invoce=json});

    LoadTableDefinition('data/t1.json');
    window.addEventListener('keydown', function (e) {

//         console.log('KeyCode: ' + e.keyCode + ', CharCode: ' + e.charCode + ', Cnt : ' + i + ' ,Which : ' + e.which)

         var CellSelected=$('td.selected','#table');
         var RowSelected=CellSelected.parent('tr');
         var IndexColumn=CellSelected.parent('tr').find('td').index(CellSelected);
         var Input=$('input',CellSelected);
         switch (e.keyCode){
         case 37 :{
             if (CellSelected.prev('td').length != 0 && Input.length==0) {
                 CellSelected.toggleClass('selected').prevAll('td:first').toggleClass('selected')
             }
             break;
         }
         case 39 : {
             if (CellSelected.next('td').length != 0 && Input.length==0) {
                 CellSelected.toggleClass('selected').nextAll('td:first').toggleClass('selected')
             }
             break;
         }
         case 38 :{

             if (RowSelected.prev('tr').length!=0 && RowSelected.prev('tr').find('td').length!=0 && Input.length==0) {
                 $($(RowSelected.prev('tr'))
                     .find('td')[RowSelected.find('td').index(CellSelected.toggleClass('selected'))])
                     .toggleClass('selected');
             }
           //  ($(RowSelected).prev('tr').length!=0) ? $(RowSelected).prev('tr').find('td').index(CellSelected.toggleClass('selected')).toggleClass('selected') : {};
            break;
         }
             case 40 :{

                 if (RowSelected.next('tr').length!=0 && Input.length==0) {
                     $($(RowSelected.next('tr'))
                         .find('td')[RowSelected.find('td').index(CellSelected.toggleClass('selected'))])
                         .toggleClass('selected');
//                     RowSelected.next('tr').toggleClass('tr.selected');

                 }
             break;
             }
             case 13 :{
                 if (Input.length!=0){
                     Input.replaceWith(Input.val());
                 } else {
                     CellSelected.wrapInner('<input type="text" class="editor">').find('input').val(CellSelected.text()).focus().select();
                 }
             }
             case 27 :{
                 if (Input.length!=0){
                     Input.replaceWith(CellSelected.text());
                 }
             }
         default : {
         };
         }
        var triggerDown=(+window.innerHeight-CellSelected.get(0).getBoundingClientRect().top-$('#table:parent').offset().top);//);
        var triggerUp=CellSelected.get(0).getBoundingClientRect().top-$('#table:parent').offset().top;+CellSelected.height()-$('#header').height();
        console.log("clientrect : "+(window.innerHeight));
        console.log( "Margin : "+$('#table:parent').offset().top);
        console.info('top cell : '+CellSelected.get(0).getBoundingClientRect().top)

        if (triggerDown<0)
        {console.log('trigered')
            window.scrollTo(0,$(window).scrollTop()-triggerDown)};
        if(triggerUp<0) {
            window.scrollTo(0,$(window).scrollTop()+triggerUp)};

        if ([13,37,38,39,40].indexOf(e.keyCode)>0 && Input.length==0) {
//            console.log(e.keyCode);

                e.preventDefault ? e.preventDefault() : (e.returnValue = false);
            return false

        }
        return true
   });
//    window.addEventListener('click',function(e){console.log(e.target.tagName)})
    $('#table').delegate('td','click',function(){
        $('td.selected',$('#table')).toggleClass('selected');
        $(this).toggleClass('selected');
        });
});





function LoadTableDefinition(src)
{
    $.getJSON(src,function(data)
    {
        
        var colcnt=data.Header.Columns.length;
        var j=0;
        $('<tr></tr>').appendTo('table#table');
        $.each(data.Header.Columns, function(key,value)
        {
          $('<th width="'+value.width+'%">'+value.caption+'</th>').appendTo('table#table>tbody>tr');
        });
        for (var i = 1; i < 100; i++)
        {
            $('<tr></tr>').insertAfter('tr:last').data("LineNo",i);
            for (var col = 0; col < colcnt; col++)
            {
          $('<td class="tbody">'+(j++)+'</td>').appendTo('tr:last');
            }
        }
        $('td:last').removeClass('tbody').addClass('selected').html('sssssss');
    });
    
}